package com.tencreat.common.utils.pay;

import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AliPayWithdrawalUtil {
    // 支付宝应用appid
    private static final String appid = "你的应用appID";

    //支付宝应用私钥
    private static final String merchantPrivateKey = "应用私钥";

    //支付宝应用公钥证书路径
    private static final String appCertPublicKey = "应用公钥证书路径";

    //支付宝公钥证书路径
    private static final String certPublicKey = "公钥证书路径";

    //支付宝根证书路径
    private static final String rootCert = "根证书路径";

    /**
     * 支付宝提现功能
     *
     * @param withdrawal 支付宝转账参数实体类
     * @return AlipayFundTransUniTransferResponse response
     * 判断成功失败 response.isSuccess()：true 成功 ，false失败
     * 成功获取交易单号：
     * JSONObject jsonObject = JSONObject.parseObject(response.getBody());
     * String alipay_fund_trans_uni_transfer_response = jsonObject.getString("alipay_fund_trans_uni_transfer_response");
     * JSONObject parseObject = JSONObject.parseObject(alipay_fund_trans_uni_transfer_response);
     * 失败获取错误结果：response.getSubMsg()
     * 成功获取交易单号：parseObject.getString("order_id")
     */
    public static AlipayFundTransUniTransferResponse withdrawal(Withdrawal withdrawal) {
        AlipayFundTransUniTransferResponse response = null;
        try {
            //
            String rootPath = AliPayWithdrawalUtil.class.getResource("/").getPath();
            System.out.println("项目据对路径" + rootPath);
            CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
            certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
            certAlipayRequest.setAppId(appid);
            certAlipayRequest.setPrivateKey(merchantPrivateKey);
            certAlipayRequest.setFormat("json");
            certAlipayRequest.setCharset("UTF-8");
            certAlipayRequest.setSignType("RSA2");
            certAlipayRequest.setCertPath(rootPath + appCertPublicKey);
            certAlipayRequest.setAlipayPublicCertPath(rootPath + certPublicKey);
            certAlipayRequest.setRootCertPath(rootPath + rootCert);
            DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
            AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
            request.setBizContent("{" +
                    "\"out_biz_no\":\"" + withdrawal.getOutBizNo() + "\"," +
                    "\"trans_amount\":" + withdrawal.getTransAmount() + "," +
                    "\"product_code\":\"TRANS_ACCOUNT_NO_PWD\"," +
                    "\"biz_scene\":\"DIRECT_TRANSFER\"," +
                    "\"order_title\":\"" + withdrawal.getOrderTitle() + "\"," +
                    "\"payee_info\":{" +
                    "\"identity\":\"" + withdrawal.getIdentity() + "\"," +
                    "\"identity_type\":\"ALIPAY_USER_ID\"" +
                    "    }," +
                    "\"remark\":\"" + withdrawal.getRemark() + "\"," +
                    "\"business_params\":\"{\\\"payer_show_name\\\":\\\"******科技有限公司\\\"}\"," +
                    "  }");
            response = alipayClient.certificateExecute(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }


    public static void main(String[] args) {
        Withdrawal withdrawal = new Withdrawal();
        withdrawal.setTransAmount(new BigDecimal("0.1"));
        withdrawal.setRemark("提现");
        withdrawal.setOutBizNo(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        withdrawal.setOrderTitle("提现");
        withdrawal.setIdentity("2088702126358350");
        String result = "";
        AlipayFundTransUniTransferResponse response = null;
        try {
            response = withdrawal(withdrawal);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(response);
        try {
            System.out.println(AliPayWithdrawalUtil.class.getResource("/").getPath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
